package net.shippo.poc.azure.iothub;
import com.microsoft.azure.iot.service.exceptions.IotHubException;
import com.microsoft.azure.iot.service.sdk.Device;
import com.microsoft.azure.iot.service.sdk.RegistryManager;

import java.io.IOException;
import java.net.URISyntaxException;

public class CreateDeviceIdentity {
	private static final String connectionString = "HostName=iot-poc-hub.azure-devices.cn;SharedAccessKeyName=iothubowner;SharedAccessKey=OiFgE5xtwLSu/hEZtN1qXXQInP/pM/PcbKITWE/hBX4=";
	private static final String deviceId = "myFirstAndroidDevice";
	
	public static void main(String[] args)  throws IOException, URISyntaxException, Exception {
		RegistryManager registryManager = RegistryManager.createFromConnectionString(connectionString);

		Device device = Device.createFromId(deviceId, null, null);
		try {
		  device = registryManager.addDevice(device);
		} catch (IotHubException iote) {
		  try {
		    device = registryManager.getDevice(deviceId);
		  } catch (IotHubException iotf) {
		    iotf.printStackTrace();
		  }
		}
		System.out.println("Device id: " + device.getDeviceId());
		System.out.println("Device key: " + device.getPrimaryKey());
	}

}
